package com.guns.dome1.service.impl;


import com.guns.dome1.dao.IDeptDao;
import com.guns.dome1.entity.Dept;
import com.guns.dome1.service.IDeptService;
import com.guns.dome1.session.SessionService;
import com.guns.dome1.vo.req.ReqDeptVo;
import com.guns.dome1.vo.resp.RespBaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeptServiceImpl implements IDeptService {


    //dao通道
    @Autowired
    private IDeptDao iDeptDao;
    @Autowired
    private SessionService sessionService;


    //查询所有部门
    @Override
    public RespBaseVo list(ReqDeptVo reqDeptVo) {

        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqDeptVo.getSession());

        if (!check) {
            //session无效或以超时
            System.out.println("查询所用部门时超时");
            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录以超时");
            return respBaseVo;
        }

        List<Dept> deptList = iDeptDao.findAll();

        respBaseVo.setCode(0);
        respBaseVo.setMsg("查询成功");
        respBaseVo.setData(deptList);

        return respBaseVo;


    }


    //增加部门
    @Override
    public RespBaseVo add(ReqDeptVo reqDeptVo) {

        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqDeptVo.getSession());

        if (!check) {
            //session无效或以超时
            System.out.println("增加部门超时");
            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录以超时");
            return respBaseVo;
        }

        String deptName = reqDeptVo.getDeptName();
        //判断部门名称是否为空
        if (deptName == null) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("存在空值");
            return respBaseVo;

        }

        try {

            Dept dept = new Dept();
            dept.setDeptName(reqDeptVo.getDeptName());
            iDeptDao.save(dept);
            respBaseVo.setCode(0);
            respBaseVo.setMsg("新增部门成功");
            return respBaseVo;
        } catch (Exception e) {

            respBaseVo.setCode(-1);
            respBaseVo.setMsg("部门已存在");
            return respBaseVo;

        }


    }


    //修改部门
    @Override
    public RespBaseVo edit(ReqDeptVo reqDeptVo) {

        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqDeptVo.getSession());

        if (!check) {

            //session无效或以超时
            System.out.println("修改部门超时");

            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录已经超时请重新");
            return respBaseVo;
        }


        String deptName = reqDeptVo.getDeptName();
        Integer deptId = reqDeptVo.getDeptId();
        if (deptId == null) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("存在空值");
            return respBaseVo;

        }
        //判断部门名称是否为空
        if (deptName == null) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("存在空值");
            return respBaseVo;

        }

        Optional<Dept> byId = iDeptDao.findById(deptId);

        if (byId == null) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("修改部门不存在");
            return respBaseVo;
        }

        try {

            Dept dept = new Dept();

            dept.setDeptId(reqDeptVo.getDeptId());
            dept.setDeptName(reqDeptVo.getDeptName());

            iDeptDao.save(dept);

            respBaseVo.setCode(0);
            respBaseVo.setMsg("部门更新成功");
            return respBaseVo;

        } catch (Exception e) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("部门更新失败");
            return respBaseVo;
        }


    }


    //删除部门
    @Override
    public RespBaseVo del(ReqDeptVo reqDeptVo) {

        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqDeptVo.getSession());

        if (!check) {

            //session无效或以超时
            System.out.println("删除部门超时");

            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录已经超时请重新");
            return respBaseVo;
        }

        Integer deptId = reqDeptVo.getDeptId();
        Optional<Dept> byId = iDeptDao.findById(deptId);

        if (byId == null) {

            respBaseVo.setCode(-1);
            respBaseVo.setMsg("删除部门不存在");
            return respBaseVo;
        }

        try {
            iDeptDao.deleteById(deptId);
            respBaseVo.setCode(0);
            respBaseVo.setMsg("删除成功");
            return respBaseVo;
        } catch (Exception e) {

            respBaseVo.setCode(-1);
            respBaseVo.setMsg("删除失败");
            return respBaseVo;
        }


    }
}
